package cn.xiaobai.core.domain.config;

import cn.xiaobai.core.domain.factory.InConfFactory;
import cn.xiaobai.core.domain.module.InConf;
import cn.xiaobai.core.domain.schedule.TaskExecutorProcessor;
import cn.xiaobai.core.repository.PluginDataFactory;
import cn.xiaobai.core.utils.JsonUtils;
import cn.xiaobai.enums.ToolState;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.JsonNode;
import com.mybatisflex.annotation.Column;
import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.KeyType;
import com.mybatisflex.annotation.Table;
import lombok.Data;

/**
 * 工具配置
 *
 * @Author yangdaji
 * @Date 2023/12/11
 * @PackageName cn.xiaobai.core.domain.config
 * @ClassName ToolConfig
 */
@Data
@Table("tool_info")
public class ToolInfo {

    /**
     * 工具id
     */
    @Id(keyType = KeyType.Auto)
    private Integer id;

    /**
     * 工具名称
     */
    @Column("tool_name")
    private String toolName;

    /**
     * 工具类型, 和插件类型绑定
     */
    @Column("type")
    private String type;

    /**
     * 工具描述
     */
    @Column("description")
    private String description;

    /**
     * 配置项
     */
    @Column("config")
    private String config;

    /**
     * 工具状态
     */
    @Column("tool_state")
    private Integer state;

    /**
     * 配置项
     */
    @Column(ignore = true)
    private JsonNode jsonNode;
    /**
     * 工具状态
     */
    @Column(ignore = true)
    private ToolState toolState;


    /**
     * 输入配置工厂
     */
    @Column(ignore = true)
    private InConf inConf;

    public void buildInConf(PluginDataFactory pluginDataFactory) {
        InConfFactory inConfFactory = pluginDataFactory.getInConfFactory(type);
        inConf = inConfFactory.getInstance();
        inConf.init(jsonNode);
    }

    public void start(TaskExecutorProcessor taskExecutorProcessor) {
        inConf.start(taskExecutorProcessor);
    }

    public void setConfig(String config) {
        this.config = config;
        this.jsonNode = JsonUtils.toBean(config, JsonNode.class);
    }

    public void setState(Integer state) {
        this.toolState = ToolState.getState(state);
        this.state = state;
    }


}
